import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:situation/app/theme/app_theme.dart';
import 'package:situation/data/store/config_store.dart';
import 'package:situation/shared/util/util_mk.dart';
import 'package:situation/widgets/PlayVideo.dart';
import 'package:situation/widgets/mk_cell.dart';
import 'package:situation/widgets/mk_load_image.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';
import 'state.dart';

class IntellDetailPage extends StatefulWidget {
  const IntellDetailPage({Key? key}) : super(key: key);

  @override
  State<IntellDetailPage> createState() => _IntellDetailPageState();
}

class _IntellDetailPageState extends State<IntellDetailPage> {
  final IntellDetailController controller = Get.put(IntellDetailController());
  final IntellDetailState state = Get.find<IntellDetailController>().state;

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Scaffold(
        appBar: TDNavBar(title: '${state.intellDetail['position']}'),
        body: SingleChildScrollView(
          child: Column(
            children: [
              MkCell(title: '类型', note: '${state.intellDetail['typeStr']}'),
              MkCell(title: '位置', note: '${state.intellDetail['position']}'),
              MkCell(title: '联系方式', note: '${state.intellDetail['phone']}'),

              MkCell(title: '备注', note: '${state.intellDetail['remark']}'),
              MkCell(title: '附件信息'),
              state.intellDetail['annexs'] == null
                  ? Container()
                  : Container(
                    alignment: Alignment.centerLeft,
                    child: Wrap(
                      alignment: WrapAlignment.start,
                      spacing: 8,
                      children: [
                        ...state.intellDetail['annexs'].map((e) {
                          return _buildImageBox(e);
                        }).toList(),
                      ],
                    ),
                  ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildImageBox(e) {
    return e['type'] == 1
        ? GestureDetector(
          onTap: () {
            TDImageViewer.showImageViewer(
              context: context,
              images: [ConfigStore.to.baseFileUrl + e['path']],
            );
          },
          child: Stack(
            children: [
              MkLoadImage(
                ConfigStore.to.baseFileUrl + e['path'],
                width: (Get.width - 60) / 4,
                height: (Get.width - 60) / 4,
              ),
            ],
          ),
        )
        : GestureDetector(
          onTap: () {
            UtilMk.openBottomSheet(
              480,
              "视频",
              Container(
                height: 400,
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(
                    AppTheme.iDefaultRadius * 2,
                  ),
                ),
                child: PlayVideo(url: ConfigStore.to.baseFileUrl + e['path']),
              ),
            );
          },
          child: Stack(
            alignment: Alignment.center,
            children: [
              MkLoadImage(
                ConfigStore.to.baseFileUrl + e['coverImg'],
                width: (Get.width - 60) / 4,
                height: (Get.width - 60) / 4,
              ),
              Icon(Icons.play_circle, color: Colors.blueAccent),
            ],
          ),
        );
  }

  @override
  void dispose() {
    Get.delete<IntellDetailController>();
    super.dispose();
  }
}
